<template>
  <div class="manage-warn">
    <div class="warn-header">
      <el-button type="primary" size="small" @click="dialogFormVisible = true"
        >添加告警组</el-button
      >
    </div>
    <el-table :data="warnlist" style="width: 100%">
      <el-table-column prop="name" label="告警组"> </el-table-column>
      <el-table-column prop="category_name" label="告警类型"> </el-table-column>
      <el-table-column prop="content" label="URL"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-popover
            popper-class="managewarn"
            style="min-width: 10px!imporant"
            placement="top-start"
            trigger="hover"
            content="发送测试"
          >
            <el-button
              slot="reference"
              type="success"
              icon=" el-icon-s-promotion"
              size="mini"
              circle
              @click="testSend(scope.row)"
            ></el-button>
          </el-popover>
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.row)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; margin-top: 8px">
      <el-pagination
        :current-page="pageParams.page"
        :total="totalCount"
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :title="title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" label-position="right">
        <el-form-item label="告警组:" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="告警类型:" prop="category">
          <el-select v-model="form.category" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in warnTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="URL:" prop="content">
          <el-input v-model="form.content"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addWarn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "ManageWarn",
  data() {
    return {
      pageParams: {
        page: 1,
        page_size: 10
      },
      totalCount: 0,
      warnlist: [{ prop: 2 }],
      warnTypeList: [],
      dialogFormVisible: false,
      form: {
        category: "",
        name: "",
        content: ""
      },
      rules: {
        name: [{ required: true, message: "请输入告警组", trigger: "blur" }],
        category: [{ required: true, message: "请选择告警类型", trigger: "change" }],
        content: [{ required: true, message: "请输入URL", trigger: "blur" }]
      }
    };
  },
  computed: {
    title() {
      return !this.form.id ? "添加告警组" : "编辑告警组";
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.$refs.form.resetFields();
        this.form = {
          category: "",
          name: "",
          content: ""
        };
      }
    }
  },
  created() {
    this.initData();
    this.$http.warn.getSelectList().then(res => {
      this.warnTypeList = res;
    });
  },

  methods: {
    testSend(row) {
      this.$http.warn.sendWarn({ alert_id: row.id }).then(res => {
        this.$message({
          message: "发送成功",
          type: "success"
        });
      });
    },
    handleSizeChange(val) {
      this.pageParams.page_size = val;
      this.initData();
    },
    handleCurrentChange(val) {
      this.pageParams.page = val;
      this.initData();
    },
    initData() {
      this.$http.warn
        .getWarnList({
          project_id: this.$route.query.project_id,
          ...this.pageParams
        })
        .then(res => {
          this.warnlist = res.items;
          this.totalCount = res.total;
        });
    },
    edit(row) {
      this.$http.warn.getWarnById({ lert_id: row.id }).then(res => {
        this.form = res;
      });
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      this.$http.warn
        .delWarn({
          alert_id: row.id
        })
        .then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        });
    },
    addWarn() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.project_id = this.$route.query.project_id;
          if (!this.form.id) {
            this.$http.warn.addWarn({ ...this.form }).then(res => {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.initData();
            });
          } else {
            this.$http.warn.updateWarn({ alert_id: this.form.id, ...this.form }).then(res => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.initData();
            });
          }

          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.manage-warn {
  .warn-header {
    float: right;
    margin-bottom: 8px;
  }
}

/deep/.el-table__header-wrapper .el-table__header th {
  background-color: #f8f8f9;
  color: #515a6e;
  font-size: 14px;
  font-weight: 500;
}
</style>
<style>
.el-popper.managewarn {
  min-width: 10px !important;
  padding: 10px !important;
}
</style>
